package adventure.commands;

import adventure.*;
import player.*;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class Tell extends AbstractCommand {

    public Tell(Adventure a) {
        super(a, new String[]{"TELL"});
    }

    @Override
    public String validateAndExecute(Player p, String[] args) {
        if (args.length == 1) {
            return "Tell who what?";
        } else if (args.length == 2) {
            Player target = players.getOnlinePlayer(args[1]);
            return "Tell " + target.getAliveName() + " what?";
        } else {
            Player targetPlayer = players.getOnlinePlayer(args[1]);
            String message = args[2];

            for (int x = 3; x < args.length; x++) {
                message += " " + args[x];
            }

            return tell(p, targetPlayer, message);
        }
    }

    private String tell(Player player, Player target, String message) {
        if (target.getID() > 0) {
            if (message.length() > 0) {
                out.broadcastToPlayer(target,
                        ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                        + ColourUtil.colourise(" tells you '", ColourUtil.white)
                        + message
                        + ColourUtil.colourise("'", ColourUtil.white));
                return ColourUtil.colourise("You tell ", ColourUtil.white)
                        + ColourUtil.colourise(target.getName(), ColourUtil.magenta)
                        + ColourUtil.colourise(" '", ColourUtil.white)
                        + message
                        + ColourUtil.colourise("'", ColourUtil.white);
            } else {
                return "Tell " + target.getName() + " what?";
            }

        } else {
            return "There is no player by that name currently connected!";
        }
    }
}
